<?php
//子账号管理
if(!defined('IN_T')){
   die('hacking attempt');
}

//编辑、添加子账号
if($act=='edit'){
	$aid = intval($_REQUEST['aid']);
	$row = $Db->query("select * from ".$Base->table('admin_tongji')." where id=$aid",'Row');
	if(empty($_POST)){
		$row['priv'] = $Json->decode($row['priv']);
		$tp->assign('row',$row);
	}
	else{
		$data = array(
			'name'=>Common::sfilter($_POST['name']),
			'passwd'=>Common::sfilter($_POST['passwd']),
			'priv'=>$_POST['priv'],
		);
		$res['status'] = 0;
		
		if(empty($data['name'])){
			$res['msg'] = '请填写登录账号';
		}
		else if(empty($data['priv'])){
			$res['msg'] = '请至少分配一个权限';
		}
		else{
			//不编辑密码，unset掉
			if(empty($data['passwd'])){
				unset($data['passwd']);	 
			}
			else{
				$data['passwd'] = Common::encrypt($data['passwd']);
			}	
			$data['priv'] = $Json->encode($data['priv']);
			//编辑
			if($row){
				$Db->update($Base->table('admin_tongji'),$data,array('id'=>$row['id']));
				$res = array('status'=>1,'msg'=>'编辑成功','href'=>'/'.TONGJI_PATH.'/?m=member');
			}
			//添加
			else{
				if(empty($data['passwd'])){
					$res['msg'] = '请填写登录密码';
				}
				else{
					$Db->insert($Base->table('admin_tongji'),$data);
					$res = array('status'=>1,'msg'=>'添加成功','href'=>'/'.TONGJI_PATH.'/?m=member');
				}
			}
		}
		echo $Json->encode($res);
		exit;
	}
}
//删除子账号
else if($act=='delete'){
	$aid = intval($_POST['aid']);
	$sql = "delete from ".$Base->table('admin_tongji')." where id=$aid";
	$Db->execSql($sql);
	echo $Json->encode(array('status'=>1));
	exit;
}
//显示子账号
else{
	$res = get_member_lists();
	//print_r($res);
	$tp->assign('res',$res);	
}
$tp->assign('nav','子账号管理');

//提取全景评论
function get_member_lists(){
	global $Base,$tp,$Db;
	$sql = "select * from ".$Base->table('admin_tongji')." order by id desc";
	$res = $Db->query($sql);
	foreach($res as $k=>$v){
		$res[$k]['priv'] = $GLOBALS['Json']->decode($v['priv']); 
	}
	return $res;
}
?>